package net.lzzy.service;


import net.lzzy.entity.Comment;
import net.lzzy.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class ApiCommentService {
    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private RedisTemplate redisTemplate;

    public Comment findById(Integer id){
        Object object = redisTemplate.opsForValue().get("test"+id);
        if(object != null) {
            return (Comment) object;
        }else{
            Optional<Comment> comment = commentRepository.findById(id);
            if(comment.isPresent()) {
                redisTemplate.opsForValue().set("test" + id, comment.get());
                return comment.get();
            }else{
                return null;
            }
        }
    }
}
